package frontend

import (
	"LeastMall/models"
	"time"

	"github.com/astaxie/beego"
)

type IndexController struct {
	BaseController //继承
}

func (c *IndexController) Get() {
	c.BaseInit()
	startTime := time.Now().UnixNano()
	//获取banner(轮播图)
	banner := []models.Banner{}

	if hasbanner := models.CacheDb.Get("banner", &banner); hasbanner {
		c.Data["bannerList"] = banner
	} else {
		models.DB.Where("status = 1 AND banner_type = 1").Order("sort desc").Find(&banner)
		c.Data["bannerList"] = banner
		models.CacheDb.Set("banner", banner)
	}
	//获取手机商品列表（product)
	redisPhone := []models.Product{}

	if hasPhone := models.CacheDb.Get("phone", &redisPhone); hasPhone {
		c.Data["phoneList"] = redisPhone
	} else {
		phone := models.GetProductByCategory(1, "hot", 8)
		c.Data["phoneList"] = phone
		models.CacheDb.Set("phone", phone)
	}

	//获取电视商品列表（product)
	redisTv := []models.Product{}

	if hasredisTv := models.CacheDb.Get("tv", &redisTv); hasredisTv {
		c.Data["tvList"] = redisTv

	} else {
		tv := models.GetProductByCategory(4, "best", 8)
		c.Data["tvList"] = tv
		models.CacheDb.Set("tv", tv)
	}

	endTime := time.Now().UnixNano()
	beego.Info("IndexController执行时间：", endTime-startTime)
	c.TplName = "frontend/index/index.html"
}
